#include "stdafx.h"
#include "Profiler.h"
#include "ProfilerSection.h"


CProfiler::CProfiler()
{

}

void CProfiler::Update()
{
	m_output = "$4Called functions:";

	std::stable_sort(m_sections.begin(), m_sections.end(), ProfilerSectionsPredicate);
	for (uint i = 0; i < m_sections.size(); ++i)
	{
		CProfilerSection* pSection = m_sections[i];

		uint calls = pSection->GetCallsCount();
		if (calls != 0)
		{
			float time = pSection->GetTime()*1000;	//	convert to milliseconds
			const char* name = pSection->GetName();

			string text = string_utils::format("\n$2%4u/$9%6.2f\t$7%s", calls, time, name);
			m_output.append(text.c_str());

			pSection->ResetCallCounter();
		}
	}
}

void CProfiler::Render(IRender* pRender)
{
	pRender->GetFont()->RenderText(m_output.c_str(), 250, 10, Color::Orange);
}

void CProfiler::RegisterSection(CProfilerSection* section)
{
	assert(section);
	m_sections.push_back(section);
}

bool CProfiler::ProfilerSectionsPredicate(const CProfilerSection* section_1, const CProfilerSection* section_2)
{
	return section_1->GetTime() > section_2->GetTime();
}